//
//  PostViewController.swift
//  Post
//
//  Created by 郑东喜 on 2016/11/10.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

class PostViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //(1）设置请求路径
        let url:NSURL = NSURL(string:addressUrl)!//不需要传递参数
        
        //(2) 创建请求对象
        let request:NSMutableURLRequest = NSMutableURLRequest(url: url as URL) //默认为get请求
        request.timeoutInterval = 5.0 //设置请求超时为5秒
        request.httpMethod = "POST"  //设置请求方法
        
        let token = localSave.object(forKey: userToken) as! String
        
        print(token)
        //设置请求体
//        let param:NSString = NSString(format:"oldpwd=%@&newpwd=%@&token=%@",Encryption.md5Encrypt(with: "123456"),Encryption.md5Encrypt(with: "123"),token)
        
//        let param:NSString = NSString(format:"token=%@&ac=%@&ty=%@&province=&@&city=%@&area=%@&addrs=%@&shrname=%@&shrphone=%@&qq=%@&default=%@",token,"add","0","福建省","福州市","鼓楼区","连江县温馨从","郑东喜","18905036476","946737816","1")
        
        
        let param:NSString = NSString(format:"token=%@&ac=%@",token,"list")
        
        //把拼接后的字符串转换为data，设置请求体
        request.httpBody = param.data(using: String.Encoding.utf8.rawValue)
        
        //(3) 发送请求
        NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue:OperationQueue()) { (res, data, error)in
            //返回主线程执行
            DispatchQueue.main.sync {
                //服务器返回：请求方式 = POST，返回数据格式 = JSON，用户名 = 123，密码 = 123
//                let str = NSString(data: data!, encoding:String.Encoding.utf8.rawValue)
                
                do
                {
                    let dic = try JSONSerialization.jsonObject(with: data! as Data, options: JSONSerialization.ReadingOptions.allowFragments) as! NSDictionary
                    print(dic)
                } catch {
                    
                }
                
                print(res!)
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    


}
